<?php

require_once 'az_ImageProcess.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of az_FileImage
 *
 * @author Shark
 */
class az_FileImage {

//put your code here
    var $fileId;
    var $file;
    var $name;
    var $date;
    var $type_file;
    var $name_file_type;
    var $size;
    var $user_id;
    var $dirSaveRoot = '../../../upload/image/';
    var $dirSaveResize = '../../../upload/image/';
//Table name
    var $nametbl = 'az_file';

    /*
     *  Function create image
     */
    var $imgProcess;

    public function __construct() {
        
    }

    /*
     * Funciton create object image process();
     */

    function create_object_processer() {
        $this->imgProcess = new Az_ImageProcess();
    }

    /*
     * Function create object image
     */

    function create_image($_fileId, $_file, $_name, $_date, $_type_file, $_name_file_type, $_size, $_userId) {
        $this->fileId = $_fileId;
        $this->file = $_file;
        $this->name = $_name;
        $this->date = $_date;
        $this->type_file = $_type_file;
        $this->name_file_type = $_name_file_type;
        $this->size = $_size;
        $this->user_id = $_userId;
    }

    /*
     * Function add image
     */

    function add_image() {
        global $az_db;
        if ($this->file != null) {
            if ($this->name_file_type == 'jpeg') {
                $this->dirSaveRoot.= 'jpg/root/';
                $this->dirSaveResize.= 'jpg/resize/';
            }
            if ($this->name_file_type == 'gif') {
                $this->dirSaveRoot.= 'gif/root/';
                $this->dirSaveResize.= 'jpg/resize/';
            }
            if ($this->name_file_type == 'png') {
                $this->dirSaveRoot.= 'png/root/';
                $this->dirSaveResize.= 'jpg/resize/';
            }

            //set source dir
            $this->create_object_processer();
            $this->imgProcess->destDir = $this->dirSaveRoot;
            $this->imgProcess->source = $this->file;
            if ($this->imgProcess->other_upload()) {

                //Get info of image 
                $dir_img = $this->dirSaveRoot.$this->name;
                $infoData = getimagesize($dir_img);
                $_info = '(' . $infoData[0] . ',' . $infoData[1] . ')';
                
                $sql = "INSERT INTO " . $this->nametbl .
                        "(`name`,`info`,`date`,`type_file`,`name_file_type`," .
                        "`size`,`user_id`)" .
                        "VALUES(?,?,?,?,?,?,?)";
                $params = array($this->name, $_info, date('d/m/Y'), $this->type_file, $this->name_file_type, $this->size, $this->user_id);
                if ($az_db->executeStmt_Az($sql, 'sssisii', $params)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return $this->imgProcess->getError();
            }
        }
    }

    /*
     * Function delete image
     */

    function delete_image($imgId) {
        if ($imgId != null && is_numeric($imgId)) {
            global $az_db;
            $sql = "DELETE FROM " . $this->nametbl . " WHERE file_id =" . $imgId;
            if ($az_db->executeSQL_Query($sql)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
    /*
     * Get one image infomation
     */
    function get_one_image($imgId){
        global $az_db;
        $sql = "SELECT * FROM ".$this->nametbl . " WHERE file_id =".$imgId;
        $result = $az_db->executeSQL_Query($sql);    
        if($result->num_rows > 0){
            return $result->fetch_array(MYSQLI_ASSOC);
        }else{
            return null;
        }
    }

}

?>
